package com.sailing.face.handle;

import com.alibaba.fastjson.JSON;
import com.sailing.face.config.AppConfig;
import com.sailing.face.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author fox
 */
@Component
public class FaceInterceptor implements HandlerInterceptor{

    /*@Autowired
    private AppConfig appConfig;*/

    /**
     *
     * @param req
     * @param resp
     * @param o
     * @return
     * @throws Exception
     * 添加拦截，判断请求头文件中的包名是否与服务端设定好的包名是否相同，通过拦截进入请求的controller,如果出现异常及
     * 包名错误，则返回错误码。
     */
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        String appId = req.getHeader("appId");
        if(!AppConfig.appId.equals(appId)){
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().write(JSON.toJSONString(new ResponseResult(ResponseResult.RESPONSE_CODE_FAILD,"头参数异常")));
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
